home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1998 November: Tool Chest / Dev.CD Nov 98 TC.toast / Sample Code / Snippets / Sound / SndPlayDoubleBuffer / _source / ULAW.c < prev    next >
Encoding:
C/C++ Source or Header  |  1996-11-15  |  3.8 KB  |  121 lines  |  [TEXT/CWIE]

  1. /*
  2. **    Apple Macintosh Developer Technical Support
  3. **
  4. **    Routine demonstrating how to parse Sun's .au sound files.
  5. **
  6. **    by Mark Cookson, Apple Developer Technical Support
  7. **
  8. **    File:    ULAW.c
  9. **
  10. **    Copyright ©1996 Apple Computer, Inc.
  11. **    All rights reserved.
  12. **
  13. **    You may incorporate this sample code into your applications without
  14. **    restriction, though the sample code has been provided "AS IS" and the
  15. **    responsibility for its operation is 100% yours.  However, what you are
  16. **    not permitted to do is to redistribute the source as "Apple Sample
  17. **    Code" after having made changes. If you're going to re-distribute the
  18. **    source, we require that you make it clear in the source that the code
  19. **    was descended from Apple Sample Code, but that you've made changes.
  20. */
  21.  
  22. #include "ULAW.h"
  23.  
  24. /* I threw this together in about an hour after just looking at the structure
  25.    of some .au files that I made from some known AIFF files.  This could use
  26.    some work to get it to deal with formats that I have no idea about, but
  27.    since I have no idea about them, I can't do that...
  28. */
  29.  
  30. /*-----------------------------------------------------------------------*/
  31.         OSErr    ASoundGetULAWHeader        (SoundInfoPtr theSoundInfo,
  32.                                         long *dataStart,
  33.                                         long *length)
  34. /*-----------------------------------------------------------------------*/
  35. {
  36.     ParamBlockRec            pb;
  37.     auHeader                ULAWHeader;
  38.     long                    filePosition        = kInit,
  39.                             byteCount            = kInit,
  40.                             sampleSize            = kInit,
  41.                             tempLength            = kInit;
  42.     Fixed                    sampleRate            = kInit;
  43.     OSErr                    err                    = noErr;
  44.  
  45.     *dataStart = kInit;
  46.  
  47.     err = SetFPos (theSoundInfo->refNum, fsFromStart, filePosition);
  48.     if (err != noErr) {
  49.         DebugPrint ("\pSetFPos failed!");
  50.     }
  51.     else {
  52.         byteCount = sizeof (auHeader);
  53.         err = FSRead (theSoundInfo->refNum, &byteCount, &ULAWHeader);
  54.         if ((err != noErr) && (err != eofErr)) {
  55.             DebugPrint ("\pFSRead failed!");
  56.         }
  57.         else {
  58.             sampleRate = ASoundLongDoubleToFix (ULAWHeader.sampleRate);
  59.             *dataStart = ULAWHeader.offsetToData;
  60.             *length = ULAWHeader.dataSize;
  61.  
  62.             /* Get the length of the file because SoundEdit stores the uncompressed length of a file
  63.                and not all Sun/NeXT files have the compressed length of the file in the dataSize
  64.                field.  We depend on having the right length (the compressed length). */
  65.  
  66.             pb.ioParam.ioCompletion    = nil;
  67.             pb.ioParam.ioRefNum        = theSoundInfo->refNum;
  68.             err = PBGetEOF (&pb, false);
  69.             err = pb.ioParam.ioResult;
  70.             if (err == noErr) {
  71.                 tempLength = (long)pb.ioParam.ioMisc;
  72.             }
  73.             if (*length > tempLength || *length == -1) {    /* This would mean that the uncompressed length was stored || no length was stored */
  74.                 *length = tempLength - ULAWHeader.offsetToData;    /* We want the compressed length */
  75.             }
  76.  
  77.             switch (ULAWHeader.dataFormat) {
  78.                 case SND_FORMAT_MULAW_8:
  79.                     err = SetupDBHeader (theSoundInfo,
  80.                                         sampleRate,
  81.                                         k16BitSample,
  82.                                         ULAWHeader.numChannels,
  83.                                         fixedCompression,
  84.                                         kULawSubType);
  85.                     theSoundInfo->needsMasking = false;
  86.                     break;
  87.                 case SND_FORMAT_LINEAR_8:
  88.                     err = SetupDBHeader (theSoundInfo,
  89.                                         sampleRate,
  90.                                         k8BitSample,
  91.                                         ULAWHeader.numChannels,
  92.                                         notCompressed,
  93.                                         NoneType);
  94.                     theSoundInfo->needsMasking = true;
  95.                     break;
  96.                 case SND_FORMAT_LINEAR_16:
  97.                     err = SetupDBHeader (theSoundInfo,
  98.                                         sampleRate,
  99.                                         k16BitSample,
  100.                                         ULAWHeader.numChannels,
  101.                                         notCompressed,
  102.                                         NoneType);
  103.                     theSoundInfo->needsMasking = false;
  104.                     break;
  105.                 default:
  106.                     DebugPrint ("\pUnknown or unplayable encoding format");
  107.                     err = kUnknownFormat;
  108.                     break;
  109.             }
  110.         }
  111.     }
  112.  
  113.     if (err != noErr) {
  114.         DebugPrint ("\pError in ASoundGetULAWHeader");
  115.     }
  116.  
  117.     *length += *dataStart;    /* Otherwise we wouldn't read the last few bytes from the end of the sound. */
  118.  
  119.     return err;
  120. }
  121.